package com.lovo.project.servlet.role;

import com.lovo.project.enums.UserCodeEnum;
import com.lovo.project.exception.OurException;
import com.lovo.project.model.RoleDO;
import com.lovo.project.service.RoleService;
import com.lovo.project.service.impl.RoleServiceImpl;
import com.lovo.project.vo.ResultVO;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

/**
 * @title 修改角色
 * @author: luo
 * @date: 2020/11/5 22:20
 */
@WebServlet(name = "UpdateRoleServlet",urlPatterns = "/role/updateRole")
public class UpdateRoleServlet extends HttpServlet {

    private RoleService roleService = new RoleServiceImpl();

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String roleName = request.getParameter("roleName");
        String roleDescribe = request.getParameter("roleDescribe");
        Integer id = Integer.valueOf(request.getParameter("roleId"));
        RoleDO roleDO = new RoleDO();
        roleDO.setRoleId(id);
        roleDO.setRoleName(roleName);
        roleDO.setRoleDescribe(roleDescribe);
        PrintWriter out = response.getWriter();
        try {
            roleService.updateRole(roleDO);
            out.print(ResultVO.success().toJson());
        } catch (OurException e) {
            out.print(ResultVO.fail(e.getCodeEnum()).toJson());
        }catch (Exception e1){
            out.print(ResultVO.fail(UserCodeEnum.USER_UNKNOWN_ERROR).toJson());
        }finally {
            out.close();
        }
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request,response);
    }



}
